import { requestModel, registerEntityType } from '../base'
import { registerEntityTable } from '../table'
import { ATSRouteType, type ATSRoute } from './types'
import { initMap } from './map'
import { initATSRouteMapItems } from './map-item'
import { initATSRouteIndex } from './atsroute-index'
import { registerAppMenu } from '@/app'
import { selectAndShowWaypointATSRouteTable } from './util'
import ATSRouteFilterVue from './ATSRouteFilter.vue'

export * from './types'
export * from './feature'
export { atsRouteMapItem } from './map-item'

function setupATSRouteTable() {
  registerEntityTable<ATSRoute>({
    type: 'ATSRoute',
    columns: [
      {
        name: 'id',
        cell: (e) => e.id,
        width: 100
      },
      {
        name: '起始航路点',
        cell: (e) => e.waypointList[0],
        width: 100
      },
      {
        name: '结束航路点',
        cell: (e) => e.waypointList[e.waypointList.length - 1],
        width: 100
      },
      {
        name: '航路点数',
        cell: (e) => e.waypointList.length,
        width: 100
      }
    ],
    filter: ATSRouteFilterVue
  })
}

export async function setupATSRoute() {
  const data = (await requestModel(ATSRouteType)) as ATSRoute[]
  registerEntityType<ATSRoute>({
    type: 'ATSRoute',
    typeName: '航路',
    data
  })
  setupATSRouteTable()
  initATSRouteIndex(data)
  initMap()
  initATSRouteMapItems()

  registerAppMenu({
    path: '模型/wpt:{mas-icon-ATSRoute}航路点对应航路',
    onClick: selectAndShowWaypointATSRouteTable
  })
}
